package cn.edu.tju.elm.model.entity;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import cn.edu.tju.elm.common.constants.TransactionType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Entity
@Table(name = "wallet_transaction")
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Where(clause = "is_deleted=false")
@DynamicUpdate
public class WalletTransaction extends BaseEntity {

    @Column(nullable = false)
    private LocalDateTime transactionTime;

    @Column(precision = 15, scale = 2, nullable = false)
    private BigDecimal amount;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private TransactionType transactionType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "from_wallet_id")
    private VirtualWallet fromWallet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "to_wallet_id")
    private VirtualWallet toWallet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id")
    private Order order;    // 关联订单ID（如果是支付或退款）

    private String remark;

    // 预置创建【充值】方法
    public static WalletTransaction createRecharge(User creator, VirtualWallet wallet, BigDecimal amount, String remark) {
        LocalDateTime now = LocalDateTime.now();
        return WalletTransaction.builder()
                .transactionTime(now)
                .amount(amount)
                .transactionType(TransactionType.RECHARGE)
                .toWallet(wallet)
                .remark(remark)
                .createTime(now)
                .updateTime(now)
                .creator(creator.getId())
                .updater(creator.getId())
                .deleted(false)
                .build();
    }

    // 预置创建【支付】方法
    public static WalletTransaction createPayment(User creator, VirtualWallet fromWallet, VirtualWallet toWallet,
                                                  BigDecimal amount, Order order, String remark) {
        LocalDateTime now = LocalDateTime.now();
        return WalletTransaction.builder()
                .transactionTime(now)
                .amount(amount)
                .transactionType(TransactionType.PAYMENT)
                .fromWallet(fromWallet)
                .toWallet(toWallet)
                .order(order)
                .remark(remark)
                .createTime(now)
                .updateTime(now)
                .creator(creator.getId())
                .updater(creator.getId())
                .deleted(false)
                .build();
    }

    // 预置创建【提现】方法
    public static WalletTransaction createWithdraw(User creator, VirtualWallet fromWallet, BigDecimal amount, String remark) {
        LocalDateTime now = LocalDateTime.now();
        return WalletTransaction.builder()
                .transactionTime(now)
                .amount(amount)
                .transactionType(TransactionType.WITHDRAW)
                .fromWallet(fromWallet)
                .remark(remark)
                .createTime(now)
                .updateTime(now)
                .creator(creator.getId())
                .updater(creator.getId())
                .deleted(false)
                .build();
    }

    // 预置创建【解冻】方法
    public static WalletTransaction createUnfreeze(User creator, Order order, VirtualWallet businessWallet) {
        LocalDateTime now = LocalDateTime.now();
        return WalletTransaction.builder()
                .transactionTime(LocalDateTime.now())
                .amount(order.getOrderTotal())
                .transactionType(TransactionType.UNFREEZE)
                .toWallet(businessWallet)
                .order(order)
                .remark("确认收货解冻")
                .createTime(now)
                .updateTime(now)
                .creator(creator.getId())
                .updater(creator.getId())
                .deleted(false)
                .build();
    }
}